﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.Materials;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.MaterialsCommand.MaterialsCommand;
using Yitter.IdGenerator;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.MaterialsCommandCommand.MaterialsCommandHandler
{
    /// <summary>
    /// 物料基本档添加命令处理方法
    /// </summary>
    public class Material_BasicAddCommandHandler:IRequestHandler<Material_BasicAddCommand,APIResult<object>>
    {
        /// <summary>
        /// 物料基本档仓储接口
        /// </summary>
        private readonly IBaseRepository<Material_BasicModel> mRepo;
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mRepo"></param>
        /// <param name="mapper"></param>
        public Material_BasicAddCommandHandler(IBaseRepository<Material_BasicModel>mRepo,IMapper mapper)
        {
            this.mRepo = mRepo;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async  Task<APIResult<object>> Handle(Material_BasicAddCommand request, CancellationToken cancellationToken)
        {

            APIResult<object> result = new APIResult<object>();
            var model = mapper.Map<Material_BasicModel>(request);
            model.material_code = YitIdHelper.NextId();
            var r = await mRepo.Add(model);
            if (r > 0)
            {
                result.Code = APIEnum.添加成功;
                result.Msg = APIEnum.添加成功.ToString();
                result.data = model;
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return await Task.FromResult(result);

        }
    }
}
